/**
 * [2] 两数相加
 *
 * @format
 * @lc app=leetcode.cn id=2 lang=javascript
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
// var addTwoNumbers = function (l1, l2) {
//     const num = []
//     let [min, max] = [6, 7]
//     let carryBit = 0
//     let temp = 0
//     l1.length > l2.length ? ([min, max] = [l2, l1]) : ([min, max] = [l1, l2])
//     for (let i = 0; i < min.length; i++) {
//         temp = min[i] + max[i] + carryBit
//         num[i](temp % 10)
//         carryBit = parseInt(temp / 10)
//     }
//     for (let i = min.length; i < max.length; i++) {
//         temp = max[i] + carryBit
//         num[i](temp % 10)
//         carryBit = parseInt(temp / 10)
//     }
//     return num
// }

function addTwoNumbers(l1, l2) {
    const list = { value: 0, next: null }
    let sum = 0
    let carry = 0
    let node = list
    while (l1 !== null || l2 !== null) {
        sum =
            (l1 !== null ? l1.value : 0) + (l2 !== null ? l2.value : 0) + carry
        carry = parseInt(sum / 10)
        node.value = sum % 10
        node.next = {}
        node = node.next
        l1 = l1.next
        l2 = l2.next
    }
    return list
}

const l1 = { value: 0, next: { value: 1, next: { value: 2, next: null } } }

// @lc code=end
